package view_X_O;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 09.07.14
 * Time: 20:38
 * To change this template use File | Settings | File Templates.
 */
public class FrameCub implements Runnable{
    private int flag;

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    private int useValue;
    private int compValue;
    private boolean b = true;
    private static final String PATH = "";

    @Override
    public void run() {

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension scrin = kit.getScreenSize();
        final JFrame f1 = new JFrame();
        f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f1.setSize(scrin.width / 2, scrin.height / 2);
        f1.setLocation(scrin.width/4,scrin.height/4);
        f1.setResizable(false);

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel p1 = new JPanel(new BorderLayout());
        JPanel p2 = new JPanel(new BorderLayout());

        final JButton butUser = new JButton("бросить кость за себя");
        butUser.setLocation(5,5);
        final JButton butComp = new JButton("бросить кость за комп");
        butComp.setLocation(40,5);
        p1.add(butUser, BorderLayout.WEST);
        p1.add(butComp, BorderLayout.EAST);

        final JLabel info = new JLabel();

        final JLabel l1 = new JLabel();

        butUser.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                RandomCub use = new RandomCub();
                l1.setIcon(new ImageIcon(use.randPath()));
                useValue = use.getValue();
                butUser.setVisible(!b);
            }
        });


        final JLabel l2 = new JLabel();

        butComp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                RandomCub com = new RandomCub();
                l2.setIcon(new ImageIcon((com.randPath())));
                compValue = com.getValue();
                String s = "";
                if (useValue < compValue) {
                    butComp.setVisible(!b);
                    s = "Не расстраивайся...Жизнь - боль...";
                    setFlag(0);

                }
                if (useValue > compValue) {
                    butComp.setVisible(!b);
                    s = "Ах ты жулик! Как это у тебя получилось?";
                    setFlag(1);
                    notify();

                }
                if (useValue == compValue) {
                    s = "Че ждешь? Пробуй ещё раз.";
                    butUser.setVisible(b);
                    butComp.setVisible(b);
                }
                info.setText("      "+useValue + " || " + compValue + " " + s);

            }
        });



        p2.add(l1, BorderLayout.WEST);
        p2.add(l2, BorderLayout.EAST);


        mainPanel.add(p1, BorderLayout.NORTH);
        mainPanel.add(info, BorderLayout.CENTER);
        mainPanel.add(p2, BorderLayout.SOUTH);
        f1.add(mainPanel);
        f1.setVisible(true); //To change body of implemented methods use File | Settings | File Templates.
    }
}
